@using PolicyService.Api.Commands
@using PolicyService.Api.Commands.Dtos
@using BlazorWasmClient.Shared
<CardBody>
    <CardText>Enter the data needed to calculate the price</CardText>
</CardBody>
<CardBody>
    <Validations Mode="ValidationMode.Auto" ValidateOnLoad="true" StatusChanged="ValidationStatusChanged">
    <Field Horizontal>
        <FieldLabel ColumnSize="ColumnSize.Is2">Policy from</FieldLabel>
        <FieldBody ColumnSize="ColumnSize.Is10">
            <Validation Validator="CustomValidations.NotEmptyDate">
                <DateEdit
                    TValue="DateTime?"
                    @bind-Date="@OfferParams.PolicyFrom"
                    ReadOnly="@OfferParamsReadOnly">
                    <Feedback>
                        <ValidationError>Enter valid date!</ValidationError>
                    </Feedback>
                </DateEdit>
            </Validation>
        </FieldBody>
    </Field>

    <Field Horizontal>
        <FieldLabel ColumnSize="ColumnSize.Is2">Policy to</FieldLabel>
        <FieldBody ColumnSize="ColumnSize.Is10">
            <Validation Validator="CustomValidations.NotEmptyDate">
                <DateEdit
                    TValue="DateTime?"
                    ReadOnly="@OfferParamsReadOnly"
                    @bind-Date="@OfferParams.PolicyTo">
                    <Feedback>
                        <ValidationError>Enter valid date!</ValidationError>
                    </Feedback>
                </DateEdit>
            </Validation>
        </FieldBody>
    </Field>

    @foreach (var answer in OfferParams.QuestionAnswers)
    {
        <Field Horizontal>
            <FieldLabel ColumnSize="ColumnSize.Is2">@answer.Question</FieldLabel>
            @if (answer.Answer.QuestionType == QuestionType.Numeric)
            {
                <FieldBody ColumnSize="ColumnSize.Is10">
                    <Validation Validator="CustomValidations.NotEmptyDecimal">
                        <NumericEdit
                            ReadOnly="@OfferParamsReadOnly"
                            TValue="decimal?"
                            ValueChanged="@(args => OfferParams.SetNumericAnswer(answer.Answer.QuestionCode, args))">
                            <Feedback>
                                <ValidationError>Enter valid number!</ValidationError>
                            </Feedback>
                        </NumericEdit>
                    </Validation>
                </FieldBody>
            }

            @if (answer.Answer.QuestionType == QuestionType.Choice)
            {
                <FieldBody ColumnSize="ColumnSize.Is10">
                    <Validation Validator="ValidationRule.IsSelected">
                    <Select
                        ReadOnly="@OfferParamsReadOnly"
                        TValue="string"
                        SelectedValueChanged="@(args => OfferParams.SetChoiceAnswer(answer.Answer.QuestionCode, args))">
                        <SelectItem TValue="string" Value="null"></SelectItem>
                        @foreach (var choice in answer.Choices)
                        {
                            <SelectItem TValue="string" Value="choice.Code">@choice.Label</SelectItem>
                        }
                    </Select>
                    </Validation>
                </FieldBody>
            }
        </Field>
    }

    @if (ShowPrice)
    {
        <Field Horizontal>
            <FieldLabel ColumnSize="ColumnSize.Is2" TextWeight="TextWeight.Bold">Price</FieldLabel>
            <FieldBody ColumnSize="ColumnSize.Is10" TextWeight="TextWeight.Bold">
                @Offer.TotalPrice EUR
            </FieldBody>
        </Field>
    }
    </Validations>
</CardBody>

@code {
    [Parameter]
    public OfferParametersModel OfferParams { get; set; }
    [Parameter]
    public bool ShowPrice { get; set; }
    [Parameter]
    public CreateOfferResult Offer { get; set; }
    [Parameter]
    public bool OfferParamsReadOnly { get; set; }
    public bool IsValid { get; private set; } = false;

    private Task ValidationStatusChanged(ValidationsStatusChangedEventArgs eventArgs )
    {
        IsValid = eventArgs.Status == ValidationStatus.Success; 
        return Task.CompletedTask;
    }
}